home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’89 / gadlife / source (ugly) / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-14  |  9.2 KB  |  380 lines  |  [TEXT/KAHL]

  1. #include "main.h"
  2. #include "menus.h"
  3.  
  4. extern int            isBackground;
  5. extern WindowPtr    frontUserWindow();
  6.  
  7. /*******************************************************
  8. *                                                                *
  9. *    GetMenus is called at startup time to load the menu resources and store their    *
  10. *    handles for later use.                                            *
  11. *                                                                *
  12. *******************************************************/
  13.  
  14. getMenus()
  15.     {
  16.     Handle        theMBar;
  17.     
  18.     theMBar = GetNewMBar( mBarID );
  19.     SetMenuBar( theMBar );
  20.     AddResMenu( GetMHandle( appleID ), 'DRVR' );
  21.     DrawMenuBar();
  22.     }
  23.     
  24. /*******************************************************
  25. *                                                                *
  26. *    DoCommand executes the selected menu command, and returns an integer        *
  27. *    telling whether a quit command was given.  The parameter is the longword        *
  28. *    returned by either MenuSelect or MenuKey.                            *
  29. *                                                                *
  30. *******************************************************/
  31.  
  32. int doCommand( p )
  33.     long int p;
  34.     {
  35.     int result = 1, theMenu = HiWord(p), theCommand = LoWord(p);
  36.  
  37.     switch ( theMenu )
  38.         {
  39.         case appleID:
  40.             doApple( theCommand );
  41.             break;
  42.         case fileID:
  43.             result = doFile( theCommand );
  44.             break;
  45.         case editID:
  46.             if ( !SystemEdit( theCommand ))
  47.                 doEdit( theCommand );
  48.             break;
  49.         case camID:
  50.             doCam( theCommand );
  51.             break;
  52.         case toolID:
  53.             doTool( theCommand );
  54.             break;
  55.         default: ;
  56.         }
  57.     HiliteMenu(0);
  58.     return( result );
  59.     }
  60.  
  61. doApple( num )
  62.     int num;
  63.     {
  64.     Str255        itemName;
  65.     WindowPeek    theWindow;
  66.     int            isDA;
  67.  
  68.     if ( num>2 )
  69.         {
  70.         theWindow = ( WindowPeek )FrontWindow();
  71.         isDA = theWindow && theWindow->windowKind < 0;
  72.         if( !isDA ) exportScrap();
  73.         GetItem( GetMHandle( appleID ), num, itemName );    
  74.         installModalTrap();
  75.         OpenDeskAcc( itemName );
  76.         removeModalTrap();
  77.         if( !isDA ) setBadScrap();
  78.         }
  79.     else doAbout();
  80.     }
  81.     
  82. doFile( num )
  83.     int            num;
  84.     {
  85.     int            result = -1;
  86.     WindowPtr    theWindow;
  87.     
  88.      switch (num)
  89.         {
  90.         case fmNew:
  91.             doNew( dataStatLine );
  92.             break;
  93.         case fmClose:
  94.             theWindow = FrontWindow();
  95.             if( theWindow ) doClose( theWindow );
  96.             break;
  97.         case fmQuit:
  98.             doAllClose();
  99.             killChannels();
  100.             exportScrap();
  101.             result = 0;
  102.             break;
  103.         default: ;
  104.         }
  105.     return( result );
  106.     }
  107.  
  108. doEdModify( theData, theCommand )
  109.     dataPtr        theData;
  110.     int            theCommand;
  111. {
  112.     BitMap        *changeBits;
  113.     char            *dest;
  114.     long            nextGen;
  115.     int            oldState, newState, needsDraw;
  116.  
  117.     oldState = stopData( theData, forceStop );
  118.     newState = oldState;
  119.     if( theData->isSelect ) {
  120.         changeBits = &( theData->selBits );
  121.         dest = theData->selBits.baseAddr;
  122.     } else {
  123.         changeBits = &( theData->offBits );
  124.         dest = theData->bases[ 1 ];
  125.     }
  126.     switch( theCommand ) {
  127.         case    edErase:
  128.             newState = dataStopped;
  129.             eraseData( changeBits, dest );
  130.             nextGen = 0;
  131.             break;
  132.         case    edFill:
  133.             newState = dataStopped;
  134.             fillData( changeBits, dest );
  135.             nextGen = 0;
  136.             break;
  137.         case    edInvert:
  138.             invertData( changeBits, dest );
  139.             nextGen = theData->generation;
  140.             break;
  141.         case    edRandomize:
  142.             randomize( changeBits, dest );
  143.             nextGen = 0;
  144.             break;
  145.         case edUndo:
  146.             newState = dataStopped;
  147.             nextGen = theData->undoGeneration;
  148.             break;
  149.         default: ;
  150.     }
  151.     if( !theData->isSelect ) {
  152.         swapPlane( theData );
  153.         theData->undoGeneration = theData->generation;
  154.         theData->generation = nextGen;
  155.         if( theData->cycleFlags ) startCycle( theData );
  156.         restartData( theData, newState );
  157.         needsDraw = bitsBadData + bitsBadGen;
  158.         if( theCommand != edUndo ) theData->undoFlags |= undoData;
  159.     } else {
  160.         restartData( theData, oldState );
  161.         needsDraw = bitsBadSel;
  162.     }
  163.     theData->changed = timer();
  164.     return( needsDraw );
  165. }
  166.  
  167. doEdit( theCommand )
  168.     int theCommand;
  169. {
  170.     WindowPeek    theWindow = ( WindowPeek )frontUserWindow();
  171.     dataHandle    theDataHand;
  172.     dataPtr        theData;
  173.     int            toDraw = 0;
  174.     
  175.     if( theWindow && theWindow == ( WindowPeek )FrontWindow() ) {
  176.         theDataHand = ( dataHandle )GetWRefCon( theWindow );
  177.         HLock( theDataHand );
  178.         theData = *theDataHand;
  179.         if( theCommand == edUndo || theCommand > edClear )
  180.             toDraw = doEdModify( theData, theCommand );
  181.         else switch( theCommand ) {
  182.             case edPaste:
  183.                 toDraw = doEdPaste( theData );
  184.                 break;
  185.             case edCopy:
  186.             case edCut:
  187.                 if( theData->isSelect )
  188.                     setMyClip( &( theData->selBits ), theData->selRgn );
  189.                 if( theCommand == edCopy ) break;
  190.             case edClear:
  191.                 toDraw = doEdClear( theData );
  192.                 break;
  193.             default: ;
  194.         }
  195.         HUnlock( theDataHand );
  196.         if( toDraw & bitsBadAnts ) {
  197.             activeAnts( theDataHand, -1 );
  198.             toDraw -= bitsBadAnts;
  199.         }
  200.         if( toDraw ) {
  201.             HLock( theDataHand );
  202.             displayData( *theDataHand, toDraw, NULL );
  203.             HUnlock( theDataHand );
  204.         }
  205.     }
  206. }
  207.  
  208. startCycle( theData )
  209.     dataPtr    theData;
  210. {
  211.     theData->cycleGeneration = theData->generation;
  212.     theData->cycleFitLength = theData->cycleLength;
  213.     if( theData->cycleFlags && theData->cycleBase )
  214.         DisposHandle( theData->cycleBase );
  215.     PtrToHand( theData->bases[ 0 ], &theData->cycleBase, theData->planeSize );
  216.     if( theData->cycleBase ) HPurge( theData->cycleBase );
  217. }
  218.  
  219. stopCycle( theData )
  220.     dataPtr    theData;
  221. {
  222.     if( theData->cycleBase ) DisposHandle( theData->cycleBase );
  223. }
  224.  
  225. doCam( theCommand )
  226.     int            theCommand;
  227.     {
  228.     WindowPtr    theWindow;
  229.     dataHandle    theDataHand;
  230.     dataPtr        theData;
  231.     
  232.     theWindow = frontUserWindow();
  233.     if( !theWindow ) return;                            /* should never happen */
  234.     theDataHand = ( dataHandle )GetWRefCon( theWindow );
  235.     HLock( theDataHand );
  236.     theData = *theDataHand;
  237.     switch( theCommand )
  238.         {
  239.         case camGo:
  240.             if( theData->state == dataStopped ) ++isBackground;
  241.             theData->state = dataRunning;
  242.             break;
  243.         case camStep:
  244.             if( theData->state == dataStopped ) ++isBackground;
  245.             theData->state = dataStepping;
  246.             break;
  247.         case camStop:
  248.             stopData( theData, dontForceStop );
  249.             break;
  250.         case camRestart:
  251.             if( !theData->cycleFlags ) startCycle( theData );
  252.             theData->cycleFlags ^= cycleRestart;
  253.             if( !theData->cycleFlags ) stopCycle( theData );
  254.             break;
  255.         case camDetect:
  256.             if( !theData->cycleFlags ) startCycle( theData );
  257.             theData->cycleFlags ^= cycleDetect;
  258.             if( !theData->cycleFlags ) stopCycle( theData );
  259.             break;
  260.         default: ;
  261.         }
  262.     HUnlock( theDataHand );
  263.     }
  264.  
  265. doTool( theCommand )
  266.     int    theCommand;
  267.     {
  268.     setToolCurs( theCommand );
  269.     }
  270.  
  271. /*******************************************************
  272. *                                                                *
  273. *    This gets called before doing a MenuSelect or a MenuKey to make sure that    *
  274. *    everything is checked or enabled properly.  This is vitally important - in most    *
  275. *    places I check anyway, but sometimes I depend on the fact that some option    *
  276. *    can only be selected when it is appropriate.                            *
  277. *                                                                *
  278. *******************************************************/
  279.  
  280. updateMenus()
  281.     {
  282.     WindowPtr    theWindow;
  283.     
  284.     theWindow = FrontWindow();
  285.     fixFileMenu( theWindow );
  286.     fixEditMenu( theWindow );
  287.     fixLifeMenu( theWindow );
  288.     fixToolMenu( theWindow );
  289.     }
  290.  
  291. /*******************************************************
  292. *                                                                *
  293. *    This is a hack - if I wanted to do it better, I could read the old value and then    *
  294. *    make a call for all those that had changed.  Even better, each I would keep the    *
  295. *    old values internally, and never touch the menuInfo struct.  However, this is    *
  296. *    much simpler.  If it ever breaks, I'll just fix it or something. :->            *
  297. *                                                                *
  298. *******************************************************/
  299.  
  300. changeEnable( menu, new )
  301.     int    menu;
  302.     long    new;
  303. {
  304.     ( **GetMHandle( menu )).enableFlags = new;
  305. }
  306.  
  307. fixFileMenu( theWindow )
  308.     WindowPeek    theWindow;
  309.     {
  310.     MenuHandle    theMenu;
  311.     static int        wasClose = 0;
  312.     int            isClose;
  313.     
  314.     theMenu = GetMHandle( fileID );
  315.     isClose = theWindow && theWindow->goAwayFlag;
  316.     if( !isClose && wasClose ) DisableItem( theMenu, fmClose );
  317.     else if( isClose && !wasClose ) EnableItem( theMenu, fmClose );
  318.     wasClose = isClose;
  319.     }
  320.  
  321. fixEditMenu( theWindow )
  322.     WindowPeek    theWindow;
  323.     {
  324.     dataHandle    theDataHand = NULL;
  325.     long            new = -1L ^ edGrayBits;
  326.     int            isWindow, isDA, isSelection, isUndo;
  327.  
  328.     isWindow = theWindow ? -1 : 0;
  329.     isDA = isWindow && theWindow->windowKind < userKind;
  330.     if( isWindow && !isDA ) theDataHand = ( dataHandle )GetWRefCon( theWindow );
  331.     isSelection = isWindow && theWindow->windowKind == userKind &&
  332.             ( **theDataHand ).isSelect;
  333.     isUndo = isWindow && ( isDA || ( **theDataHand ).undoFlags != undoNone );
  334.     if( !isUndo ) new ^= edUndoBit;
  335.     if( !( couldPaste() || isDA ) || !isWindow ) new ^= edPasteBit;
  336.     if( !( isSelection || isDA ) || !isWindow ) new ^= edSelectBits;
  337.     if( isDA || !isWindow ) new ^= edModifyBits;
  338.     changeEnable( editID, new );
  339.     }
  340.  
  341. fixLifeMenu( theWindow )
  342.     WindowPeek    theWindow;
  343. {
  344.     MenuHandle    theMenu;
  345.     dataHandle    theDataHand;
  346.     dataPtr        theData;
  347.     long            new = -1L ^ camGrayBits;
  348.  
  349.     if( theWindow && theWindow->windowKind >= userKind ) {
  350.         theDataHand = ( dataHandle )GetWRefCon( theWindow );
  351.         HLock( theDataHand );
  352.         theData = *theDataHand;
  353.         if( theData->state == dataRunning ) new ^= camGoBit;
  354.         if( theData->state == dataStepping ) new ^= camStepBit;
  355.         if( theData->state == dataStopped ) new ^= camStopBit;
  356.         theMenu = GetMHandle( camID );
  357.         CheckItem( theMenu, camRestart, theData->cycleFlags & cycleRestart );
  358.         CheckItem( theMenu, camDetect, theData->cycleFlags & cycleDetect );
  359.         HUnlock( theDataHand );
  360.     } else new ^= camGoBit + camStepBit + camStopBit + camFlagBits;
  361.     changeEnable( camID, new );
  362. }
  363.  
  364. fixToolMenu()
  365.     {
  366.     MenuHandle    theMenu;
  367.     static int        oldTool = 0;
  368.     int            newTool;
  369.     
  370.     newTool = getToolCurs();
  371.     if( oldTool != newTool )
  372.         {
  373.         theMenu = GetMHandle( toolID );
  374.         if( oldTool ) CheckItem( theMenu, oldTool, 0 );
  375.         CheckItem( theMenu, newTool, -1 );
  376.         oldTool = newTool;
  377.         }
  378.     }
  379.  
  380.